package com.gx303.wximageselect;

import java.io.File;
import java.util.ArrayList;

import com.gx303.wximageselect.bean.Image;
import com.gx303.wxselectlibrary.R;
import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 预览
 * @author gaoxiang
 *@Time 2015-6-4
 */
public class PopWxImageSelectPreview extends Activity {

	ImageView iv_back;
	ImageView iv_select;
	Button btn_send;
	android.support.v4.view.ViewPager vp1;
	public static String EXTRA_DATA="now_select_data";
	public static String EXTRA_SELECT_IMAGE="select_image";
	public static String EXTRA_NOW_SELECT_INDEX="now_select_index";
	 public   ArrayList<Image> images ;
	 public  ArrayList<Image> selectImages;
	 int nowSelectIndex=0;
	 ArrayList<View> views;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pop_wx_image_select_preview);
		
		images=(ArrayList<Image>)getIntent().getSerializableExtra(EXTRA_DATA);
		selectImages=(ArrayList<Image>)getIntent().getSerializableExtra(EXTRA_SELECT_IMAGE);
		Log.e("Unity", "images"+images.size()+" selectImages:"+selectImages.size());
		nowSelectIndex=getIntent().getIntExtra(EXTRA_NOW_SELECT_INDEX, 0);
		init();
		LayoutInflater lf = getLayoutInflater().from(this);  
		
		views=new ArrayList<View>();
		for(int i=0;i<images.size();i++)
		{
			View view1 = lf.inflate(R.layout.list_item_wx_image_select_preview_vp, null);
			views.add(view1);
		}
		myAdapter adapter1=new myAdapter();
		vp1.setAdapter(adapter1);
		vp1.setCurrentItem(nowSelectIndex);
		setSelectImage(nowSelectIndex);
		Log.e("Unity", "aaa"+images.size());
		vp1.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				setSelectImage(arg0);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		iv_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				exit();
			}
		});
		iv_select.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int index=vp1.getCurrentItem();
				Image nowImage=images.get(index);
				if(!selectImages.contains(nowImage)&&selectImages.size()>=datas.MAX_SELECT_IMAGE_NUM)
				{
					//当等于这个个数时，不可以再选
					Toast.makeText(getApplicationContext(), getResources().getString(R.string.wx_image_select_msg_amount_limit), Toast.LENGTH_SHORT).show();
				}
				else
				{
					SelectImage(nowImage);
					setSelectImage(index);
				}
			}
		});
		btn_send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent it_back=new Intent();
				it_back.putExtra(EXTRA_SELECT_IMAGE, selectImages);
				setResult(RESULT_OK,it_back);
				finish();
			}
		});
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
//		super.onBackPressed();
		exit();
	}
	public void exit()
	{

		Intent it_back=new Intent();
		it_back.putExtra(EXTRA_SELECT_IMAGE, selectImages);
		setResult(RESULT_CANCELED,it_back);
		finish();
	}
	public void setSelectImage(int index)
	{

//		int index=vp1.getCurrentItem();
		Image image1=images.get(index);
		Log.e("Unity", "index:"+index+"image1:"+image1.path+"selectImages:"+selectImages.size());
		if(selectImages.contains(image1))
		{
			iv_select.setImageDrawable(getResources().getDrawable(R.drawable.wx_image_select_select));
		}
		else
		{
			iv_select.setImageDrawable(getResources().getDrawable(R.drawable.wx_image_select_unselect));
		}
	}
	public  void SelectImage(Image i1)
	{
		if(selectImages.contains(i1))
		{
			selectImages.remove(i1);
		}
		else
		{
			selectImages.add(i1);
		}
	}
	public void init()
	{
		iv_back=(ImageView)findViewById(R.id.iv_back);
		iv_select=(ImageView)findViewById(R.id.iv_select);
		btn_send=(Button)findViewById(R.id.btn_send);
		vp1=(android.support.v4.view.ViewPager)findViewById(R.id.vPager);
	}
	public class myAdapter extends PagerAdapter
	{
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0==arg1;
		}
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
//			super.destroyItem(container, position, object);
			 container.removeView(views.get(position));  
		}
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			View view1=views.get(position);
			container.addView(view1);  
			ImageView iv1=(ImageView)view1.findViewById(R.id.imageView1);
			File imageFile = new File(images.get(position).path);
			Picasso.with(getApplicationContext())
            .load(imageFile)
            .placeholder(R.drawable.default_error)
                    //.error(R.drawable.default_error)
//            .resize(picWidth, viewHeight)
//            .centerCrop()
            .into(iv1);
			return view1;
		}
		
	};

	
}
